package com.yzy.baselibrary.base

import android.app.Activity
import android.content.Context
import android.graphics.Color
import android.os.Bundle
import android.view.View
import android.view.WindowManager
import androidx.lifecycle.LifecycleOwner
import com.yzy.baselibrary.extention.screenHeight
import com.yzy.baselibrary.extention.screenWidth
import com.yzy.baselibrary.utils.NavigationUtils
import com.yzy.baselibrary.widget.toast.AimyToastUtils
import com.blankj.utilcode.util.BarUtils
import com.trello.rxlifecycle3.components.support.RxAppCompatActivity
import org.kodein.di.Kodein
import org.kodein.di.KodeinAware
import org.kodein.di.KodeinTrigger
import org.kodein.di.android.closestKodein
import org.kodein.di.android.retainedKodein

/**
 *description: BaseActivity.
 *@date 2018/10/16 20:23.
 *@author: YinZeYu.
 */
abstract class BaseActivity : RxAppCompatActivity(), LifecycleOwner, KodeinAware {

    /** 屏幕宽度  */
    var mScreenWidth: Int = 0
    /** 屏幕高度(包含状态栏高度但不包含底部虚拟按键高度)  */
    var mScreenHeight: Int = 0
    /** 屏幕状态栏高度  */
    var mStatusBarHeight: Int = 0
    /** 弹窗,权限请求等需要用到Activity  */
    protected lateinit var mActivity: Activity
    /** 上下文  */
    protected lateinit var mContext: Context

    protected val parentKodein by closestKodein()

    override val kodeinTrigger = KodeinTrigger()

    override val kodein: Kodein by retainedKodein {
        extend(parentKodein)

    }
    /**
     * 是否需要显示状态栏
     *
     * @return true显示黑色状态栏，false不显示状态栏
     */
    protected open val isNeedShowStatusBar: Boolean
        get() = false

    /**为了解决登录页状态栏的问题*/
    protected open fun isNeedInitStatus(): Boolean {
        return true
    }

    public override fun onCreate(savedInstanceState: Bundle?) {
        this.initDefaultData()
        this.onCreateBefore()
        if (isNeedInitStatus()) {
            this.initStatus()
        }
        super.onCreate(savedInstanceState)
        if (layoutResId() != 0) {
            setContentView(layoutResId())
        }
        kodeinTrigger.trigger()
        initView()
        initData()
    }

    /**
     * 初始化部分成员变量
     */
    private fun initDefaultData() {
        //公用成员变量初始化
        mActivity = this
        mContext = this
        mScreenWidth = mContext.screenWidth()
        mScreenHeight = mContext.screenHeight()
        mStatusBarHeight = BarUtils.getStatusBarHeight()
    }

    protected open fun getNavigationBarHeight(): Int {
        return NavigationUtils.getNavigationBarHeight(mActivity)
    }

    /** 适配状态栏  */
    protected open fun initStatus() {
        val window = window
        window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR or
                View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
        if (isNeedShowStatusBar) {
            window.clearFlags(
                WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS or WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION
            )
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
            window.statusBarColor = statusBarColor()
        }
    }

    /**
     * 状态栏颜色,默认为黑色#ff000000
     */
    protected open fun statusBarColor(): Int {
        //返回透明的状态栏颜色则不填充状态栏
        return Color.TRANSPARENT
    }


    override fun onDestroy() {
        super.onDestroy()
        //移除所有Activity类型的Toast防止内存泄漏
        AimyToastUtils.cancelActivityToast(this)
    }

    /** 这里可以做一些setContentView之前的操作,如全屏、常亮、设置Navigation颜色、状态栏颜色等  */
    protected open fun onCreateBefore() {}

    /**
     * 页面内容布局resId
     */
    protected abstract fun layoutResId(): Int

    /**
     * 初始化View相关
     */
    protected abstract fun initView()

    /**
     * 初始化数据相关
     */
    protected abstract fun initData()
}
